//
//  CRMChooseCarVC.m
//  New_Runway
//
//  Created by NewLife on 2018/3/6.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CRMChooseCarVC.h"
#import "CustomerDetailCell.h"
#import "NRWCarCustomerIntentModel.h"

@interface CRMChooseCarVC ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation CRMChooseCarVC
{
    UITableView *tableView;
    NRWCarCustomerIntentModel *model;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBarView.titleLabel.text = @"意向车辆";
    
    [self setupUI];
    
    [self loadData];
}

- (void)setupUI {
    
    tableView = DPTableViewForStyle(UITableViewStylePlain)
    .setDelegate(self)
    .setDataSource(self)
    .registerCell(CustomerDetailCell.class, @"cell")
    .setTableFooterView(DPView())
    .setBackgroundColor([UIColor whiteColor])
    .setFrame(CGRectMake(0, self.navH, Screen_width, Screen_height - self.navH - self.BotH));
    
    self.view.addSubview(tableView);
}

- (void)loadData {
    
    if (self.customerShopId.length < 1) {
        return;
    }
    
    [SVProgressHUD show];
    
    [YQNetworking postWithUrl:[NSString stringWithFormat:@"%@%@", CRMContextApi, customerDetailAPI] refreshRequest:YES cache:NO params:@{@"customerShopId": self.customerShopId} progressBlock:nil successBlock:^(id response) {
        
        [SVProgressHUD dismiss];
        
        model = [NRWCarCustomerIntentModel mj_objectWithKeyValues:response];
        
        if (model.code == 1) {//成功
            
            [tableView reloadData];
            
        } else {//error
            
        }
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
        [SVProgressHUD dismiss];
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return model.result.modelList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CustomerDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    NRWCarCustomerIntentlListModel *intentListModel = model.result.modelList[indexPath.row];
    
    cell.imageUrlStr = intentListModel.carImageUrl;
    
    cell.carNameStr = intentListModel.carTitle;
    
    cell.carPriceStr = [NSString stringWithFormat:@"¥%.2f万", intentListModel.carRetailPrice];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.chooseCarcompleted) {
        
        self.chooseCarcompleted(model.result.modelList[indexPath.row]);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
